package com.catsic.zcjc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {
	
	/**
	 * 返回一天的最开始的时候,00:00:00
	 * @param date
	 * @return
	 */
	public static Date getBeginningOfADay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 返回一天的最后的时候, 23:59:59
	 * @param date
	 * @return
	 */
	public static Date getEndOfADay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	
	/**
     * 格式化输出日期
     * @param date 日期
     * @param format  格式
     * @return 返回字符型日期
     */
    public static String format(Date date, String format) {
        return format(date, format, Locale.getDefault());
    }

    /**
     * 格式化输出日期 支持Locale
    * @param date 日期
    * @param format 格式
    * @param locale 返回字符型日期
    * @return
    */
    public static String format(Date date, String format, Locale locale) {
        try {
            if (null != date) {
                java.text.DateFormat df = new SimpleDateFormat(format, locale);
                return df.format(date);
            }
        } catch (Exception e) {
        }
        return "";
    }
    
    /**
     * 返回月初时间 
     * @param date
     * @return
     */
    public static Date getBeginingOfAMonth(Date date) {
    	Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
    }
    
    /**
     * 获取月末时间
     * @param date
     * @return
     */
    public static Date getEndOfAMonth(Date date) {
    	Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
    }
    
    /**
     * 加月份
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneMonthLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.MONTH, amount);
    	return cal.getTime();
    }
    
    /**
     * 加年份
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneYearLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.YEAR, amount);
    	return cal.getTime();
    }
    
    /**
     * 加天数
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneDateLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.DATE, amount);
    	return cal.getTime();
    }
    
    /**
     * 加小时
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneHourLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.HOUR, amount);
    	return cal.getTime();
    }
    
    /**
     * 加分钟
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneMinuteLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.MINUTE, amount);
    	return cal.getTime();
    }
    
    /**
     * 加秒钟
     * @param date 
     * @param amount 
     * @return
     */
    public static Date getOneSecondLater(Date date, Integer amount) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.add(Calendar.SECOND, amount);
    	return cal.getTime();
    }
    
    public static String getDate() {
		return getDate("yyyyMMddHHmmss");
	}

	/**
	 * 
	 * @param date_mode
	 *            :yyyyMMddHHmmss
	 * @return
	 */
	public static String getDate(String date_mode) {
		Date d = new Date();
		SimpleDateFormat date = new SimpleDateFormat(date_mode);
		date.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		String datename = date.format(d).toString();
		return datename;
	}
	
	/**
	 * 格式化指定日期
	 * 
	 * @param d
	 * @param dateMode
	 * @return
	 */
	public static String formatDate(Date d, String dateMode) {
		if (d == null || dateMode == null || "".equals(dateMode.trim())) {
			return null;
		}
		SimpleDateFormat df = new SimpleDateFormat(dateMode);
		return df.format(d);
	}

	/**
	 * 格式化指定字符串日期
	 * 
	 * @param dateStr
	 * @param dateMode
	 * @return
	 */
	public static String formatDate(String dateStr, String dateMode) {
		if (dateStr == null || dateMode == null || "".equals(dateMode.trim())) {
			return null;
		}
		dateStr = dateStr.replaceAll("[^0-9]", "");
		dateStr = rightZero(dateStr, 17);
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		try {
			Date target = df.parse(dateStr);
			SimpleDateFormat df2 = new SimpleDateFormat(dateMode);
			return df2.format(target);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 格式化指定字符串日期 长度必须大于4 且为 2017xxxxxxxx形式
	 * @param dateStr 
	 * @return
	 */
	public static Date formatDate(String dateStr) {
		if (dateStr == null || dateStr.length() < 4) {
			return null;
		}
		dateStr = dateStr.replaceAll("[^0-9]", "");
		if (dateStr.length() == 4) {
			dateStr += "0101";
		} else if (dateStr.length() == 6) {
			dateStr += "01";
		}
		dateStr = rightZero(dateStr, 17);
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		try {
			return df.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 字符串转日期
	 * @param str
	 * @return
	 */
	public static Date StrToDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
		date = sdf.parse(str);
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 字符串右补0
	 * 
	 * @param str
	 * @param length
	 *            包含 str 在内字符串总长度
	 * @return
	 */
	private static String rightZero(String str, int length) {
		if (str == null) {
			return null;
		}
		int zeroLength = length - str.length();
		if (zeroLength <= 0) {
			return str;
		}
		for (int i = 0; i < zeroLength; i++) {
			str += "0";
		}
		return str;
	}
	
	/**
	 * 计算两个时间的小时数
	 * @param nowDate
	 * @param startDate
	 * @return
	 * @throws Exception
	 */
	public static long getDatePoor(Date nowDate, Date startDate) throws Exception {
		
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    // 获得两个时间的毫秒时间差异
	    long diff = nowDate.getTime() - startDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    long allHour = day * 24 + hour;
	    return allHour;
	}
	
	
	public static void main(String[] args) throws ParseException {
//	    Date now = new Date();
//	    System.out.println(now);
//	    Date date = getBeginningOfADay(now);
//	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	    System.out.println(dateFormat.format(date));
//	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	    Date aa = sdf.parse("2008-08-08 12:10:12");
//	    System.out.println(aa);
	    
	    /*String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long("1508723174000");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        Date aa = simpleDateFormat.parse(res);
        long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    // 获得两个时间的毫秒时间差异
	    Date nowDate = new Date();
	    long diff = nowDate.getTime() - aa.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    System.out.println("时间"+day);
	    System.out.println("时间"+nowDate);
	    System.out.println("时间"+aa);
	    System.out.println("时间"+(day*24 + hour));*/
	    /*
	    date = getEndOfADay(now);
	    System.out.println(dateFormat.format(date));
	    
	    System.out.println(dateFormat.format(getBeginingOfAMonth(now)));
	    System.out.println(dateFormat.format(getEndOfAMonth(now)));
	    */
	    
//	    System.out.println(DateUtils.format(DateUtils.getOneMonthLater(date), "yyyy-MM-dd HH:mm:ss"));
		
		Date date = getOneMonthLater(new Date(), -1);
		System.out.println("时间"+date);
		
    }
}
